// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Vector2Converter.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Numerics
{
    using System;
    using System.ComponentModel;
    using System.Globalization;

    /// <summary>
    /// An internal class to format Vector2
    /// </summary>
    internal class Vector2Converter : ExpandableObjectConverter
    {
        /// <summary>
        /// Convert check
        /// </summary>
        /// <param name="context">Which context</param>
        /// <param name="t">What type</param>
        /// <returns>True if convertible</returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type t)
        {
            if (t == typeof(string))
            {
                return true;
            }

            return base.CanConvertFrom(context, t);
        }

        /// <summary>
        /// Convert from helper
        /// </summary>
        /// <param name="context">Which context</param>
        /// <param name="info">Culture info</param>
        /// <param name="value">The object to convert</param>
        /// <returns>Converted object</returns>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo info, object value)
        {
            if (value is string)
            {
                string[] components = (value as string).Split(new[] { ',' });
                Vector2 val = new Vector2();
                val.X = double.Parse(components[0]);
                val.Y = double.Parse(components[1]);
                return val;
            }

            return base.ConvertFrom(context, info, value);
        }

        /// <summary>
        /// Convert to another object
        /// </summary>
        /// <param name="context">Which context</param>
        /// <param name="info">Culture info</param>
        /// <param name="value">Object to convert</param>
        /// <param name="destType">Destination type</param>
        /// <returns>Converted object</returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo info, object value, Type destType)
        {
            if (destType == typeof(string) && (value is Vector2))
            {
                Vector2 val = (Vector2)value;
                return string.Format("{0}, {1}", val.X, val.Y);
            }

            return base.ConvertTo(context, info, value, destType);
        }

        /// <summary>
        /// Returns the Vector2 properties in the order X, Y
        /// </summary>
        /// <param name="context">Which context</param>
        /// <param name="value">Object to describe</param>
        /// <param name="attributes">Object attributes</param>
        /// <returns>Descriptor collection</returns>
        public override PropertyDescriptorCollection GetProperties(
            ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            PropertyDescriptorCollection unorderedProps = base.GetProperties(context, value, attributes);
            PropertyDescriptorCollection props = new PropertyDescriptorCollection(null);
            props.Add(unorderedProps["X"]);
            props.Add(unorderedProps["Y"]);
            return props;
        }
    }
}
